var couponId=0;
$(function(){
    var timeOut = function(){  //超时函数
        $("#lotteryBtn").rotate({
            angle:0,
            duration: 10000,
            animateTo: 2160, //这里是设置请求超时后返回的角度，所以应该还是回到最原始的位置，2160是因为我要让它转6圈，就是360*6得来的
            callback:function(){
                alert('网络超时')
            }
        });
    };
    var rotateFunc = function(awards,angle,text){  //awards:奖项，angle:奖项对应的角度
        $('#lotteryBtn').stopRotate();                 //停止旋转
        $("#lotteryBtn").rotate({
            angle:0,
            duration: 5000,
            animateTo: angle+2520, //angle是图片上各奖项对应的角度，1440是我要让指针旋转4圈。所以最后的结束的角度就是这样子^^
            callback:function(){
                alert(text)
            }
        });
    };

    $("#lotteryBtn").rotate({
        bind:
        {
            click: function(){
                /*var time = [0,1];
                time = time[Math.floor(Math.random()*time.length)];*/
                /*if(time==0){
                    timeOut(); //网络超时
                }*/

                if(true){
                    var data = [1,2,3,1,0,2,0,3,0,1,0,2,3,0,1,0,2,0,3,0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,0,1,0,2,0,3,0,6,0,2,0,4,0,5,0,0,6,0]; //返回的数组
                    data = data[Math.floor(Math.random()*data.length)];
                    couponId=data;
                    if(data==1){
                        rotateFunc(1,22.5,'恭喜您抽中优惠券100')
                    }
                    if(data==2){
                        rotateFunc(2,67.5,'恭喜您抽中优惠券30元')
                    }
                    if(data==3){
                        rotateFunc(3,112.5,'恭喜您抽中优惠券10元')
                    }
                    if(data==4){
                        rotateFunc(4,157.5,'恭喜您抽中积分20')
                    }
                    if(data==5){
                        rotateFunc(5,247.5,'恭喜您抽中优惠券60元')
                    }
                    if(data==6){
                        rotateFunc(6,292.5,'恭喜您抽中优惠券500元')
                    }
                    if(data==7){
                        rotateFunc(7,337.5,'恭喜您抽中优惠券50元')
                    }
                    if(data==0){
                        rotateFunc(0,202.5,'很遗憾，这次您未抽中奖')
                    }
                }
            }
        }

    });

    choujiang(couponId,1);
})
//抽奖传入用户id和优惠券id
function choujiang(couponId,userId) {
    $.ajax({
        headers: {
            token: getCookie("token")
        },
        type: "post",
        url: "http://cn.pn.com/user/Coupon/v/addUsercoupon",
        data: {couponId: couponId, userId: userId},
        dateType: "json",
        async: false,
        success: function (data) {
            alert("领取成功!")
        }
    })
}